package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author syh
 * @create 2023-08-15 17:15
 */
@RestController
@RequestMapping("/admin/system/role")
@Tag(name = "角色管理模块")
// 统一跨域配置
//@CrossOrigin(allowCredentials = "true" , originPatterns = "*",
//        allowedHeaders="*" )
public class SysRoleController {
    @Resource
    SysRoleService sysRoleService;

    @Operation(summary = "查询所有角色并判断是否已分配")
    @GetMapping("all/{userId}")
    public Result<List<SysRole>> all(@PathVariable("userId")Long userId){
       List<SysRole> sysRoles= sysRoleService.listAll(userId);
        return Result.build(sysRoles,ResultCodeEnum.SUCCESS);
    }



    @Operation(summary = "更新角色")
    @PutMapping
    public Result update(@RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return Result.ok(null);
    }

    @Operation(summary = "根据id查询角色")
    @GetMapping("{id}")
    public Result<SysRole> getById(@PathVariable("id")Long id){
        SysRole sysRole=sysRoleService.getById(id);
        return Result.build(sysRole,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增角色")
    @PostMapping
    public Result save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return Result.ok(null);
    }




    @Operation(summary = "根据id删除角色")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id")Long id){
        sysRoleService.deletById(id);
        return Result.ok(null);
    }

    @GetMapping("page/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> page(@PathVariable("pageNum") Integer pageNum,
                                          @PathVariable("pageSize")Integer pageSize,
                                          String keyword){
        /*
            角色管理：操作的是sys_role表
            1、创建表映射的实体类SysRole
            2、创建SysRoleMapper接口
                定义带条件查询分页数据的方法
            3、创建SysRoleMapper.xml
                实现条件查询分页数据的方法
            4、创建业务类封装条件查询分页数据的代码返回数据
         */
        PageInfo<SysRole> pageInfo = sysRoleService.findPageByKeyword(pageNum, pageSize, keyword);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }
}
